<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper 事务机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .number-circle {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            flex-shrink: 0;
        }
        
        .timeline-line {
            position: absolute;
            left: 20px;
            top: 40px;
            bottom: 0;
            width: 2px;
            background: linear-gradient(to bottom, #667eea, #764ba2);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: rgba(102, 126, 234, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #667eea;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-network-wired mr-4"></i>
                Zookeeper 事务机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解分布式协调服务的核心原理</p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-atom mr-2"></i>
                    <span>原子性保证</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sort-numeric-up mr-2"></i>
                    <span>顺序性保证</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>高可靠性</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式系统中，保证数据的一致性和操作的可靠性是至关重要的挑战。Apache Zookeeper 作为一个高性能的分布式协调服务，通过精心设计的事务机制，为分布式应用提供了强大的原子性和顺序性保证。本文将深入探讨 Zookeeper 如何实现这些关键特性。
            </p>
        </div>

        <!-- Section 1: 原子性 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="number-circle">1</div>
                <h2 class="text-3xl font-bold ml-4 text-gradient">事务操作的原子性</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <p class="text-lg text-gray-700 mb-6 leading-relaxed">
                    事务操作的原子性是指要么所有操作成功，要么所有操作都不会影响系统状态。在 Zookeeper 中，这一特性通过以下精妙的机制实现：
                </p>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="card-hover bg-gray-50 rounded-xl p-6">
                        <div class="feature-icon">
                            <i class="fas fa-file-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">事务日志</h3>
                        <p class="text-gray-600">Zookeeper 使用事务日志记录所有操作。每个写请求都首先被持久化到事务日志中，确保即使系统崩溃也能恢复。</p>
                    </div>
                    
                    <div class="card-hover bg-gray-50 rounded-xl p-6">
                        <div class="feature-icon">
                            <i class="fas fa-sync-alt"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">同步处理</h3>
                        <p class="text-gray-600">所有写请求经由 Leader 节点处理，并广播给 Follower 节点。只有在多数节点确认后，操作才被认为完成。</p>
                    </div>
                    
                    <div class="card-hover bg-gray-50 rounded-xl p-6">
                        <div class="feature-icon">
                            <i class="fas fa-list-ol"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">顺序处理</h3>
                        <p class="text-gray-600">通过唯一顺序号确保请求按序执行。前一个请求完成后，后续请求才能执行，避免操作丢失或重复。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2: 顺序性 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="number-circle">2</div>
                <h2 class="text-3xl font-bold ml-4 text-gradient">事务操作的顺序性</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <p class="text-lg text-gray-700 mb-8 leading-relaxed">
                    事务操作的顺序性确保所有操作按照正确的顺序执行。Zookeeper 通过以下机制实现这一关键特性：
                </p>
                
                <div class="space-y-6">
                    <div class="flex items-start">
                        <div class="bg-purple-100 rounded-full p-3 mr-4 mt-1">
                            <i class="fas fa-crown text-purple-600"></i>
                        </div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2 text-gray-800">Leader 选举和日志复制</h3>
                            <p class="text-gray-600 leading-relaxed">使用 Paxos 协议变体选举 Leader，确保日志正确复制到所有 Follower 节点。Leader 负责处理写操作并同步日志，保证所有节点对操作顺序达成一致。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="bg-blue-100 rounded-full p-3 mr-4 mt-1">
                            <i class="fas fa-hashtag text-blue-600"></i>
                        </div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2 text-gray-800">顺序号（ZXID）</h3>
                            <p class="text-gray-600 leading-relaxed">为每个写操作分配唯一的顺序号。所有写操作按 ZXID 顺序处理，读操作可按此顺序获取最新数据状态。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="bg-green-100 rounded-full p-3 mr-4 mt-1">
                            <i class="fas fa-broadcast-tower text-green-600"></i>
                        </div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2 text-gray-800">全序广播</h3>
                            <p class="text-gray-600 leading-relaxed">所有写请求通过全序广播传递。Leader 将请求广播给所有 Follower，确保所有节点同时收到相同请求，保证操作顺序一致性。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Architecture Diagram -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">Zookeeper 事务处理架构</h2>
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <div class="mermaid">
                    graph TB
                        Client[客户端] -->|写请求| Leader[Leader节点]
                        Leader -->|事务日志| Log[(事务日志)]
                        Leader -->|全序广播| F1[Follower 1]
                        Leader -->|全序广播| F2[Follower 2]
                        Leader -->|全序广播| F3[Follower 3]
                        F1 -->|ACK| Leader
                        F2 -->|ACK| Leader
                        F3 -->|ACK| Leader
                        Leader -->|响应| Client
                        
                        style Client fill:#f9f,stroke:#333,stroke-width:2px
                        style Leader fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style Log fill:#ffd93d,stroke:#333,stroke-width:2px
                        style F1 fill:#6dd5ed,stroke:#333,stroke-width:2px
                        style F2 fill:#6dd5ed,stroke:#333,stroke-width:2px
                        style F3 fill:#6dd5ed,stroke:#333,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">
                <i class="fas fa-lightbulb mr-3 text-yellow-500"></i>
                实现原理总结
            </h2>
            
            <div class="bg-gradient-to-br from-purple-50 to-blue-50 rounded-2xl p-8">
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="relative">
                        <div class="timeline-line"></div>
                        <div class="space-y-8">
                            <div class="flex items-start relative">
                                <div class="number-circle bg-white shadow-md">1</div>
                                <div class="ml-6">
                                    <h3 class="text-lg font-bold mb-2">事务日志</h3>
                                    <p class="text-gray-600">记录所有写请求的日志，保证即使系统崩溃也能恢复数据。</p>
                                </div>
                            </div>
                            
                            <div class="flex items-start relative">
                                <div class="number-circle bg-white shadow-md">2</div>
                                <div class="ml-6">
                                    <h3 class="text-lg font-bold mb-2">同步写请求</h3>
                                    <p class="text-gray-600">通过 Leader 节点处理写请求，并广播给 Follower 节点，确保操作的原子性。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="relative">